/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.DccOss;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DccOssRepository;
import me.zhengjie.modules.system.service.DccOssService;
import me.zhengjie.modules.system.service.dto.DccOssDto;
import me.zhengjie.modules.system.service.dto.DccOssQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DccOssMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author cjl
* @date 2023-05-17
**/
@Service
@RequiredArgsConstructor
public class DccOssServiceImpl implements DccOssService {

    private final DccOssRepository dccOssRepository;
    private final DccOssMapper dccOssMapper;

    @Override
    public Map<String,Object> queryAll(DccOssQueryCriteria criteria, Pageable pageable){
        Page<DccOss> page = dccOssRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dccOssMapper::toDto));
    }

    @Override
    public List<DccOssDto> queryAll(DccOssQueryCriteria criteria){
        return dccOssMapper.toDto(dccOssRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DccOssDto findById(Integer id) {
        DccOss dccOss = dccOssRepository.findById(id).orElseGet(DccOss::new);
        ValidationUtil.isNull(dccOss.getId(),"DccOss","id",id);
        return dccOssMapper.toDto(dccOss);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DccOssDto create(DccOss resources) {
        return dccOssMapper.toDto(dccOssRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DccOss resources) {
        DccOss dccOss = dccOssRepository.findById(resources.getId()).orElseGet(DccOss::new);
        ValidationUtil.isNull( dccOss.getId(),"DccOss","id",resources.getId());
        dccOss.copy(resources);
        dccOssRepository.save(dccOss);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dccOssRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DccOssDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DccOssDto dccOss : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("状态", dccOss.getStatus());
            map.put("类型", dccOss.getType());
            map.put("描述", dccOss.getUrl());
            map.put("识别文字", dccOss.getContent());
            map.put("解析文档", dccOss.getContentBaike());
            map.put("上传ip", dccOss.getIp());
            map.put("上传者", dccOss.getOperator());
            map.put(" createdAt",  dccOss.getCreatedAt());
            map.put(" updatedAt",  dccOss.getUpdatedAt());
            map.put("外链", dccOss.getOutUrl());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}